package com.ybg.bookmember.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybg.bookcommons.dto.BookinfosDTO;
import com.ybg.bookcommons.vo.BookinfosVO;
import com.ybg.bookmember.mapper.BookTypesMapper;
import com.ybg.bookmember.mapper.BookinfosMapper;
import com.ybg.bookmember.module.entity.BookTypes;
import com.ybg.bookmember.module.entity.Bookinfos;
import com.ybg.bookmember.services.BookinfosService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BookinfosServiceImpl extends ServiceImpl<BookinfosMapper, Bookinfos>
    implements BookinfosService {

    @Resource
    private BookTypesMapper bookTypesMapper;

    @Override
    public Page<BookinfosDTO> selectBooksMutilCondition(BookinfosVO book, int currentPage, int pageSize) {
        Map<Long, String> types = bookTypesMapper.selectList(null)
                .stream().collect(Collectors.toMap(BookTypes::getTypeid, BookTypes::getTypename));

        Page page = new Page();

        List<Bookinfos> books = this.getBaseMapper().selectBooksByConds(book);

        page.setCurrent(currentPage);
        page.setSize(pageSize);
        int size=books.size();
        page.setTotal(size);
        int start = (currentPage - 1) * pageSize;
        int over = currentPage * pageSize;
        List<Bookinfos> lst = books.subList(start, over<size?over:size);
        List<BookinfosDTO> res = lst.stream().map(bk -> {
            BookinfosDTO bkdto = BookinfosDTO.builder().typename(types.get(bk.getTypeid())).build();
            BeanUtils.copyProperties(bk, bkdto);
            return bkdto;
        }).collect(Collectors.toList());
        page.setRecords(res);

        return page;
    }
}
